<template>
  <el-dialog :append-to-body="true" :visible.sync="dialog" :title="isAdd ? '新增菜单' : '编辑菜单'" width="600px">
    <el-form ref="form" :model="form" size="small" label-width="80px">
      <el-form-item label="资源名称" prop="resourceName">
        <el-input v-model="form.resourceName" placeholder="资源名称" style="width: 460px;"/>
      </el-form-item>
      <el-form-item label="菜单图标">
        <el-popover
          placement="bottom-start"
          width="460"
          trigger="click"
          @show="$refs['iconSelect'].reset()">
          <IconSelect ref="iconSelect" @selected="selected" />
          <el-input slot="reference" v-model="form.resourceIcon" style="width: 460px;" placeholder="点击选择图标" readonly>
            <svg-icon v-if="form.icon" slot="prefix" :icon-class="form.icon" class="el-input__icon" style="height: 32px;width: 16px;" />
            <i v-else slot="prefix" class="el-icon-search el-input__icon"/>
          </el-input>
        </el-popover>
      </el-form-item>
      <el-form-item label="上级资源代码" prop="presourceNo">
        <el-input v-model="form.presourceNo" placeholder="上级资源代码" style="width: 460px;"/>
      </el-form-item>
      <el-form-item label="资源代码" prop="resourceNo">
        <el-input v-model="form.resourceNo" placeholder="资源代码" style="width: 460px;"/>
      </el-form-item>
      <el-form-item label="资源类型" prop="resourceType">
        <el-radio v-model="form.resourceType" label="MENU">菜单</el-radio>
        <el-radio v-model="form.resourceType" label="BUTTON" >按钮</el-radio>
      </el-form-item>
      <el-form-item label="是否显示" prop="ifVisible">
        <el-radio v-model="form.ifVisible" label="Y">是</el-radio>
        <el-radio v-model="form.ifVisible" label="N" >否</el-radio>
      </el-form-item>
      <el-form-item label="排序" prop="sortOrder">
        <el-input v-model.number="form.sortOrder" placeholder="序号越小越靠前" style="width: 460px;"/>
      </el-form-item>
      <el-form-item label="级别" prop="resourceLevel">
        <el-input v-model.number="form.resourceLevel" placeholder="级别" style="width: 460px;"/>
      </el-form-item>
      <el-form-item label="组件路径">
        <el-input v-model="form.reqUrl" placeholder="组件路径" style="width: 460px;"/>
      </el-form-item>
      <el-form-item label="路由路径">
        <el-input v-model="form.routePath" placeholder="路由路径" style="width: 460px;"/>
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="form.memo" placeholder="备注" style="width: 460px;"/>
      </el-form-item>
      <!-- <el-form-item label="上级类目">
        <treeselect v-model="form.pid" :options="menus" style="width: 460px;" placeholder="选择上级类目" />
      </el-form-item> -->
      <!-- <el-form-item style="margin-top: -10px;margin-bottom: 0px;" label="选择角色">
        <treeselect v-model="roleIds" :multiple="true" :options="roles" style="width: 460px;" placeholder="请选择角色" />
      </el-form-item> -->
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import Treeselect from '@riophae/vue-treeselect'
import IconSelect from '@/components/IconSelect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
export default {
  components: { Treeselect, IconSelect },
  props: {
    menus: {
      type: Array,
      required: true
    },
    roles: {
      type: Array,
      required: true
    },
    isAdd: {
      type: Boolean,
      required: true
    },
    sup_this: {
      type: Object,
      default: null
    },
    presourceNo: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      loading: false, dialog: false,
      isSon: this.presourceNo,
      form: { resourceName: '', ifVisible: 'Y', memo: '', presourceNo: "", reqUrl: '', resourceNo: '', resourceType: 'MENU', routePath: '', sortOrder: '', resourceIcon: '', resourceLevel: '' },
      roleIds: [1],
      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        sort: [
          { required: true, message: '请输入序号', trigger: 'blur', type: 'number' }
        ],
        iframe: [
          { required: true, message: '请选择菜单类型', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    updateNo(code) {
      this.isSon = code
      this.form.presourceNo = code
    },
    cancel() {
      this.resetForm()
    },
    doSubmit() {
      // this.$refs['form'].validate((valid) => {
      //   if (valid) {
      //     this.loading = true
      //     this.form.roles = []
      //     const _this = this
      //     this.roleIds.forEach(function(data, index) {
      //       const role = { id: data }
      //       _this.form.roles.push(role)
      //     })
      //     if (this.isAdd) {
      //       this.doAdd()
      //     } else this.doEdit()
      //   } else {
      //     return false
      //   }
      // })
      if (this.isAdd) {
        this.doAdd()
      } else this.doEdit()
    },
    doAdd() {
      this.$api.fenxiao.menu.createMenu({
        data: this.form
      }).then(res => {
        this.resetForm()
        this.$notify({ title: '添加成功', type: 'success', duration: 2500 })
        this.loading = false
        if (this.isSon) {
          this.$parent.getMenus()
        } else {
          this.$parent.$parent.getMenus()
        }
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    doEdit() {
      this.$api.fenxiao.menu.editMenu({
        data: this.form
      }).then(res => {
        this.resetForm()
        this.$notify({ title: '修改成功', type: 'success', duration: 2500 })
        this.loading = false
        this.sup_this.getMenus()
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
      this.form = { resourceName: '', ifVisible: 'Y', memo: '', presourceNo: "", reqUrl: '', resourceNo: '', resourceType: 'MENU', routePath: '', sortOrder: '', resourceIcon: '', resourceLevel: '' }
      this.roleIds = [1]
    },
    selected(name) {
      this.form.resourceIcon = name
    }
  }
}
</script>

<style scoped>

</style>
